const VueAutoRouteWebpackPlugin = require('./config/custom-auto-routes')
// const AssetsPlugin=require('assets-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');//引入gzip压缩插件
// 代码压缩
let path = require('path')

const envConfig = require('./config/env');
const systemConfig = require('./config/system');
var outputDir = envConfig[process.env.NODE_ENV].outputDir;
var assetsDir = envConfig[process.env.NODE_ENV].assetsDir;
var htmlDir = envConfig[process.env.NODE_ENV].htmlDir;
var proxyDomain = envConfig[process.env.NODE_ENV].proxyDomain;
//获取灰度标记
let grayMark = process.env.npm_config_gray_mark;
module.exports = {
    publicPath: envConfig[process.env.NODE_ENV].publicPath,
    outputDir: outputDir,
    assetsDir: assetsDir,
    pages: {//配置多页面入口
        login: {
            entry: 'src/login.js',
            template: 'public/login.html',
            title: systemConfig.title,
            filename:htmlDir+'login.html',
        },
        index: {
            entry: 'src/main.js',
            template: 'public/index.html',
            filename:htmlDir+'index.html',
            title: systemConfig.title,
        },
    },
    devServer: {
        port:8084,
        openPage: '/index.html',
        proxy: {
            '/': {// '/api':匹配项
                target: proxyDomain,// 接口的域名
                // secure: false,// 如果是https接口，需要配置这个参数
                changeOrigin: true,
            }
        }
    },
    lintOnSave: false,
    // configureWebpack: config => {
    //   if (process.env.NODE_ENV === 'production') {
    //     // 为生产环境修改配置...
    //   } else {
    //     // 为开发环境修改配置...
    //   }
    // }
    configureWebpack: {
        output: {
            // path: path.resolve(__dirname, outputDir),
            filename: 'dist/[name].[hash].build.js',
            chunkFilename: 'dist/[name].[hash].chunk.js',
        },
        externals: {
            'vue': 'Vue',
            'vue-router': 'VueRouter',
            'axios': 'axios',
            'element-ui': 'Element',
            'babel-polyfill': 'window',
            './cptable':'var cptable'
        },
        plugins: [
            new VueAutoRouteWebpackPlugin({
                entry: path.resolve('src/views/'),
                output: path.resolve('src/router/routes.js'),
                rootComponent: 'Main',
                indexComponent: 'Index',
                useFileName: true
            }),
            new CompressionPlugin({//gzip压缩配置
                test:/\.js$|\.html$|\.css/,//匹配文件名
                threshold:10240,//对超过10kb的数据进行压缩
                deleteOriginalAssets:false,//是否删除原文件
            }),
        ]
    },
    chainWebpack: config => {
        // 移除 prefetch 插件
        config.plugins.delete('prefetch-index').delete('preload-index');
        config
            .plugin('define')
            .tap(args => {
                let apiConfig = envConfig[process.env.NODE_ENV];
                if(grayMark) {
                    //设置灰度标记，用于微服务调试
                    apiConfig.grayMark = grayMark;
                }
                args[0].API_CONFIG = JSON.stringify(apiConfig)
                return args
            })
    }

}
